﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;
using System.Data.SqlClient;

namespace PROSHOP.DAL
{
    public class BinhLuanDAL
    {
        ProshopDataContext proshop;
        public BinhLuanDAL()
        {
            proshop = new ProshopDataContext();
        }
        public bool themBinhLuan(int maSP, int maKH, String noiDung, DateTime ngayBL)
        {
            try
            {
                BinhLuan binhLuan = new BinhLuan();
                binhLuan.MaSP = maSP;
                binhLuan.MaKH = maKH;
                binhLuan.NoiDung = noiDung;
                binhLuan.NgayBL = ngayBL;
                proshop.BinhLuans.InsertOnSubmit(binhLuan);
                proshop.SubmitChanges();
                return true;
            }
            catch (Exception)
            {

                return false;
            }
        }
        public bool xoaBinhLuan(int maBL)
        {
            try
            {
                BinhLuan binhLuan = proshop.BinhLuans.SingleOrDefault(p => p.MaBL == maBL);
                if (binhLuan == null)
                {
                    return false;
                }
                proshop.BinhLuans.DeleteOnSubmit(binhLuan);
                proshop.SubmitChanges();
                return true;
            }
            catch (Exception)
            {

                return false;
            }
        }
        public bool suaBinhLuan(int maBL, String noiDung)
        {
            try
            {
                BinhLuan binhLuan = proshop.BinhLuans.SingleOrDefault(p => p.MaBL == maBL);
                binhLuan.NoiDung = noiDung;
                proshop.SubmitChanges();
                return true;
            }
            catch (Exception)
            {

                return false;
            }
        }
        public DataSet danhSachBinhLuan()
        {
            try
            {
                var s = from p in proshop.BinhLuans
                        from q in proshop.KhachHangs
                        where p.MaKH == q.MaKhachHang
                        select new
                        {
                            p.MaSP,
                            q.HoTen,
                            q.Email,
                            p.NoiDung,
                        };
                SqlCommand cmd = (SqlCommand)proshop.GetCommand(s);
                SqlDataAdapter da = new SqlDataAdapter(cmd);
                DataSet ds = new DataSet();
                da.Fill(ds);
                return ds;
            }
            catch (Exception)
            {

                return null;
            }
            
        }
    }
}